package com.ensd.mks.portal.utils;

import cn.hutool.core.util.ObjectUtil;
import com.ejy.commons.Result;
import com.ejy.commons.exception.BusinessException;
import com.ensd.mks.portal.enums.BusinessErrorEnum;

import java.util.function.Supplier;


public class FeignRequestUtil {

    public static <T> T get(Supplier<Result<T>> request, BusinessErrorEnum error, Boolean checkEmpty){
        Result<T> result = request.get();
        if(result.isSuccess()){
            if(checkEmpty){
                if(ObjectUtil.isEmpty(result.getData())){
                    throw new BusinessException(BusinessErrorEnum.CORE_SYSTEM_EXCEPTION.getCode(), BusinessErrorEnum.CORE_SYSTEM_EXCEPTION.getMsg());
                }
            }
            return result.getData();
        }
        if(error != null){
            throw new BusinessException(error.getCode(), error.getMsg());
        }
        throw new BusinessException(BusinessErrorEnum.CORE_SYSTEM_EXCEPTION.getCode(), BusinessErrorEnum.CORE_SYSTEM_EXCEPTION.getMsg());
    }
}
